<template>
  <div class="user-list-container">
    <div class="header">
      <h2>用户管理</h2>
      <div class="actions">
        <el-button type="primary" icon="Plus">添加用户</el-button>
        <el-input
          v-model="searchKeyword"
          placeholder="搜索用户..."
          class="search-input"
          clearable
          prefix-icon="Search"
        />
      </div>
    </div>
    
    <el-table :data="filteredUsers" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="email" label="邮箱" width="220" />
      <el-table-column prop="phone" label="电话" width="150" />
      <el-table-column prop="role" label="角色" width="120">
        <template #default="{ row }">
          <el-tag :type="roleTagType(row.role)">{{ row.role }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            active-value="active"
            inactive-value="inactive"
            active-text="启用"
            inactive-text="禁用"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建时间" width="180" />
      <el-table-column label="操作" width="180" fixed="right">
        <template #default>
          <el-button type="primary" size="small" icon="Edit">编辑</el-button>
          <el-button type="danger" size="small" icon="Delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :total="users.length"
        :page-size="10"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const searchKeyword = ref('')

const users = ref([
  { id: 1, name: '张三', email: 'zhangsan@example.com', phone: '13800138000', role: '管理员', status: 'active', createdAt: '2023-01-15' },
  { id: 2, name: '李四', email: 'lisi@example.com', phone: '13900139000', role: '编辑', status: 'active', createdAt: '2023-02-20' },
  { id: 3, name: '王五', email: 'wangwu@example.com', phone: '13700137000', role: '查看', status: 'inactive', createdAt: '2023-03-10' },
  { id: 4, name: '赵六', email: 'zhaoliu@example.com', phone: '13600136000', role: '管理员', status: 'active', createdAt: '2023-04-05' },
  { id: 5, name: '钱七', email: 'qianqi@example.com', phone: '13500135000', role: '编辑', status: 'active', createdAt: '2023-05-12' },
  { id: 6, name: '孙八', email: 'sunba@example.com', phone: '13400134000', role: '查看', status: 'inactive', createdAt: '2023-06-18' },
  { id: 7, name: '周九', email: 'zhoujiu@example.com', phone: '13300133000', role: '编辑', status: 'active', createdAt: '2023-07-22' },
])

const filteredUsers = computed(() => {
  if (!searchKeyword.value) return users.value
  return users.value.filter(user => 
    user.name.includes(searchKeyword.value) || 
    user.email.includes(searchKeyword.value) ||
    user.phone.includes(searchKeyword.value)
  )
})

const roleTagType = (role) => {
  const map = {
    '管理员': 'success',
    '编辑': 'primary',
    '查看': 'info'
  }
  return map[role] || 'info'
}

const handlePageChange = (page) => {
  console.log('切换到页码:', page)
}
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.actions {
  display: flex;
  gap: 15px;
}

.search-input {
  width: 250px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>